<template>
  <div>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :style="{ width: asideWidth }" class="sidebar">
        <div class="logo-container">
          <img src="http://58.213.66.34:9999/assets/logo-6rnhqK67.png" alt="Logo" class="logo-img">
          <transition name="fade">
            <span class="logo-title" v-show="!isCollapse">DataFlowPro</span>
          </transition>
        </div>

        <el-menu
            :collapse="isCollapse"
            :default-active="$route.path"
            router
            background-color="#001529"
            text-color="rgba(255, 255, 255, 0.8)"
            active-text-color="#fff"
            style="border: none"
        >
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>
          <el-submenu index="info-management">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/officeDevices" v-if="user.role === '用户'">办公用品管理</el-menu-item>
            <el-menu-item index="/notice" v-if="user.role === '管理员'">系统公告</el-menu-item>
            <el-menu-item index="/news" v-if="user.role === '管理员'">设备信息</el-menu-item>
            <el-menu-item index="/charts" v-if="user.role === '管理员'">数据分析</el-menu-item>
            <el-menu-item index="/scriptManagement" v-if="user.role === '管理员'">脚本管理</el-menu-item>
            <el-menu-item index="/devicePage" v-if="user.role === '管理员'">设备运行</el-menu-item>
            <el-submenu index="logs" v-if="user.role === '管理员'">
              <template slot="title">日志信息</template>
              <el-menu-item index="/logs/system">系统日志</el-menu-item>
              <el-menu-item index="/logs/operation">操作日志</el-menu-item>
            </el-submenu>
            <el-menu-item index="/Server" v-if="user.role === '管理员'">服务监控</el-menu-item>
            <el-menu-item index="/Map" v-if="user.role === '管理员'">办公室监控</el-menu-item>
            <el-menu-item index="/knowledge">团队知识库</el-menu-item>
          </el-submenu>
          <el-submenu index="security-management" v-if="user.role === '管理员'">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>安全管理</span>
            </template>
            <el-menu-item index="/monitor" v-if="user.role === '管理员'">监控管理</el-menu-item>
            <el-menu-item index="/doorControl" v-if="user.role === '管理员'">门禁管理</el-menu-item>
            <el-menu-item index="/firefight" v-if="user.role === '管理员'">消防管理</el-menu-item>
          </el-submenu>
          <el-submenu index="person-management" v-if="user.role === '人事'">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>物资管理</span>
            </template>
            <el-menu-item index="/user">人员管理</el-menu-item>
            <el-menu-item index="/officeDevCateGory">物资种类</el-menu-item>
            <el-menu-item index="/officeDevices">办公用品管理</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 头部 -->
        <el-header class="header">
          <div class="header-left">
            <i :class="collapseIcon" class="collapse-btn" @click="handleCollapse"></i>
            <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
              <el-breadcrumb-item :to="{ path: '/' }">主页</el-breadcrumb-item>
              <el-breadcrumb-item>{{ $route.meta.name }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="header-right">
            <el-switch v-model="isDark" active-text="幻夜" inactive-text="淡雅" @change="toggleTheme"></el-switch>
            <i class="el-icon-quanping" style="font-size: 26px; margin: 0 10px 0 15px;" @click="handleFull"></i>
            <el-dropdown>
              <span class="el-dropdown-link">{{ user.name }}</span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 主体区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  name: 'HomeView',
  data() {
    return {
      isCollapse: false,
      asideWidth: '250px',
      collapseIcon: 'el-icon-s-fold',
      user: JSON.parse(localStorage.getItem('honey-user') || '{}'),
      isDark: localStorage.getItem('theme') === 'dark'
    }
  },
  mounted() {
    if (!this.user.id) {
      this.$router.push('/login')
    }
    this.applyTheme()
  },
  methods: {
    logout() {
      localStorage.removeItem('honey-user')
      this.$router.push('/login')
    },
    handleFull() {
      document.documentElement.requestFullscreen()
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse
      this.asideWidth = this.isCollapse ? '64px' : '250px'
      this.collapseIcon = this.isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'
      this.$nextTick(() => {
        window.dispatchEvent(new Event('resize'))
      })
    },
    toggleTheme() {
      localStorage.setItem('theme', this.isDark ? 'dark' : 'light')
      this.applyTheme()
    },
    applyTheme() {
      if (this.isDark) {
        document.documentElement.classList.add('dark-theme')
        document.documentElement.style.setProperty('--bg-color', '#001529')
        document.documentElement.style.setProperty('--text-color', '#ffffff')
        document.documentElement.style.setProperty('--menu-bg', '#001529')
        document.documentElement.style.setProperty('--menu-text', '#ffffff')
      } else {
        document.documentElement.classList.remove('dark-theme')
        document.documentElement.style.setProperty('--bg-color', '#ffffff')
        document.documentElement.style.setProperty('--text-color', '#333333')
        document.documentElement.style.setProperty('--menu-bg', '#001529')
        document.documentElement.style.setProperty('--menu-text', 'rgba(255, 255, 255, 0.8)')
      }
    }
  }
}
</script>
<style scoped>
/* 侧边栏 */
.sidebar {
  min-height: 100vh;
  background-color: var(--menu-bg);
  transition: width 0.3s ease-in-out;
}

/* 侧边栏 Logo 区域 */
.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: linear-gradient(135deg, #001529, #001529);
  padding: 12px;
  border-radius: 8px;
  transition: all 0.3s ease-in-out;
  overflow: hidden;
}

.logo-img {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  transition: transform 0.3s ease-in-out;
}

.logo-container:hover .logo-img {
  transform: scale(1.1);
}

.logo-title {
  font-size: 20px;
  font-weight: bold;
  color: white;
  text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.3);
  letter-spacing: 1px;
  white-space: nowrap;
  overflow: hidden;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s ease-in-out;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

/* 头部 */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-color);
  box-shadow: 0 2px 4px rgba(0, 21, 41, 0.2);
}

/* 左侧 */
.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  font-size: 24px;
  cursor: pointer;
  transition: transform 0.3s;
}
.collapse-btn:hover {
  transform: scale(1.1);
}

.breadcrumb {
  margin-left: 20px;
}

/* 右侧 */
.header-right {
  display: flex;
  align-items: center;
}

.fullscreen-icon {
  font-size: 24px;
  cursor: pointer;
  margin-left: 20px;
}

/* 主题切换 */
:root {
  --bg-color: #001529;
  --text-color: #333333;
  --menu-bg: #001529; /* 默认菜单背景色 */
  --menu-text: rgba(255, 255, 255, 0.8); /* 默认菜单文本颜色 */
}

.dark-theme {
  --bg-color: #001529;
  --text-color: #ffffff;
  --menu-bg: #001529; /* 夜间主题菜单背景色 */
  --menu-text: #ffffff; /* 夜间主题菜单文本颜色 */
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
  transition: background-color 0.3s, color 0.3s;
}

.sidebar {
  background-color: var(--menu-bg);
}

.el-menu {
  background-color: var(--menu-bg);
  color: var(--menu-text);
}

/* 其他样式可以根据需要添加 */
</style>

